package com.zhn.evsi.task.Evsi;

import cn.hutool.core.collection.CollUtil;
import cn.hutool.core.collection.ListUtil;
import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.zhn.evsi.client.ThirdClient;
import com.zhn.evsi.common.RedisCache;
import com.zhn.evsi.domain.entity.evsi.EvsiMarketRecord;
import com.zhn.evsi.domain.dto.evsi.EvsiMarketStatDTO;
import com.zhn.evsi.mapper.EvsiInvtypesMapper;
import com.zhn.evsi.service.IEvsiMarketRecordService;
import com.zhn.evsi.utils.TimeUtil;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;

@Slf4j
@Component
public class EvsiMarketTask {

    @Resource
    private ThirdClient thirdClient;

    @Resource
    private RedisCache redisCache;

    @Resource
    private TimeUtil timeUtil;

    @Autowired
    private EvsiInvtypesMapper invtypesMapper;

    @Autowired
    private IEvsiMarketRecordService marketRecordService;

    /**
     * 定时(每一个小时)遍历一遍市场信息,并持久化
     *
     * 多线程?
     * */
    @Scheduled(cron = "0 0 22 * * ? ")
    public void MarketTask() {

        List<JSONObject> marketStatList = new ArrayList<>();
        List<EvsiMarketStatDTO> evsiMarketStatDTOList = new ArrayList<>();
        List<EvsiMarketRecord> evsiMarketRecordList = new ArrayList<>();
        List<Integer> list = invtypesMapper.selectInvtypesOnMarketTask();
        List<List<Integer>> partition = ListUtil.partition(list, 200);
        partition.forEach(v -> {
            String join = CollUtil.join(v, ",");
            List<JSONObject> marketstat = thirdClient.marketstat(join, "10000002");
            marketStatList.addAll(marketstat);
            redisCache.setCacheObject("Progress:MarketRecord:", (double) marketStatList.size() / (double) list.size());
            marketstat.forEach(k -> {
                EvsiMarketStatDTO evsiMarketStatDTO = JSON.toJavaObject(k, EvsiMarketStatDTO.class);
                evsiMarketStatDTOList.add(evsiMarketStatDTO);

            });
        });

        evsiMarketStatDTOList.forEach(v -> {
            evsiMarketRecordList.add(
                    EvsiMarketRecord.builder()
                            .typeId(v.getSell().getForQuery().getTypes().get(0))
                            .sellQuantity(v.getSell().getVolume())
                            .sellWavg(v.getSell().getWavg())
                            .sellAvg(v.getSell().getAvg())
                            .sellVariance(v.getSell().getVariance())
                            .sellStdDev(v.getSell().getStdDev())
                            .sellMedian(v.getBuy().getMedian())
                            .sellFivePercent(v.getSell().getFivePercent())
                            .sellMax(v.getBuy().getMax())
                            .sellMin(v.getSell().getMin())
                            .sellHighToLow(v.getSell().getHighToLow())
                            .sellGenerated(v.getSell().getGenerated())
                            .buyQuantity(v.getBuy().getVolume())
                            .buyWavg(v.getBuy().getWavg())
                            .buyAvg(v.getBuy().getAvg())
                            .buyVariance(v.getBuy().getVariance())
                            .buyStdDev(v.getBuy().getStdDev())
                            .buyMedian(v.getBuy().getMedian())
                            .buyFivePercent(v.getBuy().getFivePercent())
                            .buyMax(v.getBuy().getMax())
                            .buyMin(v.getSell().getMin())
                            .buyHighToLow(v.getBuy().getHighToLow())
                            .buyGenerated(v.getBuy().getGenerated())
                            .createTime(timeUtil.TimeFormatOnDay())
                            .build()
            );
        });
        marketRecordService.saveBatch(evsiMarketRecordList);

    }
}
